//
//Personal Diary Software v 1.0
//
//    made by David Godzsak
//    for ELTE IK
//
//    email: godzsak.david@gmail.com
//

#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstdlib>
#include <string>
#include <time.h>
#include "entry.h"
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>

#define _WIN32_WINNT 0x0500
#include <windows.h>

using namespace std;

void clrscr(){system("CLS");cout<<"Personal diary software v1.0"<<endl<<endl<<endl;}

void login();                                                 //login window, calls reg(), pass(), readList() or login()
    bool reg(string name);                                    //registers user, returns whether register is succesful or not
    bool pass(string &a,string b);                            //matches passwords, returns if they match or not
    void menu(List& _a,string _name,string _pass);            //writes menu and list
    void readList(ifstream &f,string _name, string _pass);    //reads data from files
string encode(string _a);                                     //encodes strings, written in List.cpp
string decode(string _a);                                     //decodes strings, written in Lis.cpp



int main()
{
    //set window height
    HWND console = GetConsoleWindow();
    RECT r;
    GetWindowRect(console, &r); //stores the console's current dimensions
    //MoveWindow(window_handle, x, y, width, height, redraw_window);
    MoveWindow(console, r.left, r.top, 1024, 600, TRUE);

    //sets up stage
    clrscr();

    //login calls readList() if login is successful
    login();

    return 0;
}

void login(){
    //variables to store login information
    string name;
    string password,fpass;


    //get user name
    cout<<"Please log in"<<endl;
    cout<<"   username: ";
    cin>>name;
    name+=".dat";

    //try to find file, if exists, you can enter your pass if not you can register new account
    ifstream f(name.c_str());
    if (f.good()) {
        getline(f,fpass);

        //succesful login, calls readList()
        if(pass(password,fpass)){
            clrscr();
            cout<<"Welcome back "<<name.substr(0,name.length()-4)<<"!"<<endl<<endl;

            readList(f,name,fpass);

        //wrong password, back to login
        }else {
            clrscr();
            cout<<"Nooo! Wrong Password!"<<endl<<endl;
            login();
        }
    //register account
    } else {
        if(reg(name)){ //calls reg() to do the registration, logs user in afterwards
            cout<<"Thanks! You can log in now!"<<endl<<endl;
            login();
        }
    }
    f.close();
}

void readList(ifstream &f,string name,string fpass){
    //temp variable to store read line, a to make a new list
    string tmp;
    List a;

    //read file, written in List.cpp >> operator
    while(getline(f,tmp)){
        tmp=decode(tmp);
        tmp>>a;
    }

    //calls menu, with name, encoded pass and the list created above
    menu(a,name,fpass);
}

void menu(List& a, string name, string fpass){
    //char to read menu
    char c,tmp;
    int which;

    //write list, written in List.cpp << operator
    cout<<a;

    cout<<endl<<endl<<"         (O)pen entry  (A)dd entry  (N)ext page  (P)revious page  (S)ave  (E)xit"<<endl<<endl<<endl<<endl;
    cout<<"What do you want to do next? (select letter from the brackets above):  ";
    cin>>c;
    //decision on what to do next
    switch(c){
        case 'O':
        case 'o':
        //open an entry
            cout<<"   Which entry do you want to open? ";
            cin>>which;
            clrscr();
            a.open(which);
        //after seeing the entry user can go back to main menu. setting up screen again, loading menu
            cout<<endl<<endl<<endl<<endl<<"Go back? ";
            cin>>tmp;
            clrscr();
            menu(a,name,fpass);
            break;
        case 'A':
        case 'a':
        //add new entry
            clrscr();
            a.add();
        //return to menu
            menu(a,name,fpass);
            break;
        case 'N':
        case 'n':
        //next page
            clrscr();
            //with getPoz we can get the position of listing, we need to decide whether it is the last page yet
            if((a.getPoz()+1)*9<a.getDb()){
                a.pozPlus(); //to list the next page, we need to increment the pozition
            } else {
                cout<<"--------"<<endl<<"---You are on the last page, there is no more."<<endl<<"--------"<<endl<<endl<<endl;
            };

            //finally rewriting menu
            menu(a,name,fpass);

            break;
        case 'P':
        case 'p':
        //previous page
            clrscr();
            //with getPoz we can get the position of listing, we need to decide whether it is the first page
            if(a.getPoz()>0){
                a.pozMinus();
            } else {
                cout<<"--------"<<endl<<"---You are on the first page, you can't go backwards."<<endl<<"--------"<<endl<<endl<<endl;
            };
            //rewriting menu
            menu(a,name,fpass);

            break;
        case 'S':
        case 's':
        //save page
            clrscr();
            a.save(name,fpass);
            cout<<"--------"<<endl<<"---Saved."<<endl<<"--------"<<endl<<endl<<endl;
            menu(a,name,fpass);

            break;
        case 'E':
        case 'e':
            //exit
            a.save(name,fpass);
            break;
        default:
            clrscr();
            cout<<"--------"<<endl<<"---ERROR! There is no such menu! Try again."<<endl<<"--------"<<endl<<endl<<endl;
            menu(a,name,fpass);
    }
};

bool pass(string &password,string fpass){
        char c;

        cout<<"   password: ";
        while ((c=_getch()) != '\n' && c != '\r'){ //read character until end line or return carriage char
            // put it onto the back of the password
            password.push_back(c);
            // output a '*' character
            _putch('*');
        }
        cout<<endl;
        //true if passwords match
        return password==decode(fpass);
}

bool reg(string _name){
    char c;
    string pass;
    //conversation
    cout<<endl;
    cout<<"This username is not registered yet."<<endl;
    cout<<"   Do you want to register this username? (y/n) ";
    cin>>c;
    //if user input is y or Y user creates a new account
    if(c=='y'||c=='Y'){
        cout<<"      Enter a password: ";
        cin>>pass;
        //create file, put password to the first line
        ofstream f(_name.c_str());
        f<<encode(pass);
        f.close();
        clrscr();
        return true;
    } else {
        clrscr();
        cout<<"Bye then!"<<endl;
    };
    return false;
}
